package com.manli.wechat.utils;

import com.alibaba.fastjson.JSONObject;
import com.manli.common.utils.HttpUtil;
import com.manli.wechat.pojo.WeChatQrcodePojo;

/**
 * 微信自定义菜单
 */
public class WeChatQrcodeUtil {

    private static final String createUrl = "https://api.weixin.qq.com/cgi-bin/qrcode/create?access_token=";
    private static final String showqrcodeUrl = "https://mp.weixin.qq.com/cgi-bin/showqrcode?ticket=";


    /**
     * 1、创建二维码ticket
     * @return
     * {"ticket":"gQH47joAAAAAAAAAASxodHRwOi8vd2VpeGluLnFxLmNvbS9xL2taZ2Z3TVRtNzJXV1Brb3ZhYmJJAAIEZ23sUwMEmm
     * 3sUw==","expire_seconds":60,"url":"http://weixin.qq.com/q/kZgfwMTm72WWPkovabbI"     }
     */
    public static JSONObject create (String accessToken , String param) throws Exception {
        System.out.println(param);
        String retJsonStr = HttpUtil.httpsPost(createUrl+ accessToken,param , "application/json");
        JSONObject jsonObject = JSONObject.parseObject(retJsonStr);
        System.out.println(jsonObject);
        return jsonObject;
    }
    /**
     * 获取临时二维码 整数
     * @return
     */
    public static String getShortTimeInterScene(Integer expire_seconds , Integer scene){
        JSONObject object = new JSONObject();
        object.put("expire_seconds",expire_seconds);
        JSONObject sceneObj = new JSONObject();
        JSONObject sceneIdObj = new JSONObject();
        sceneIdObj.put("scene_id",scene);
        sceneObj.put("scene",sceneIdObj);
        object.put("action_info",sceneObj);
        object.put("action_name","QR_SCENE");
        return object.toJSONString();
    }

    /**
     * 获取临时二维码 整数
     * @return
     */
    public static String getShortTimeStringScene(Integer expire_seconds , String scene){
        JSONObject object = new JSONObject();
        object.put("expire_seconds",expire_seconds);
        JSONObject sceneObj = new JSONObject();
        JSONObject sceneIdObj = new JSONObject();
        sceneIdObj.put("scene_str",scene);
        sceneObj.put("scene",sceneIdObj);
        object.put("action_info",sceneObj);
        object.put("action_name","QR_STR_SCENE");
        return object.toJSONString();
    }

    /**
     * 永久二维码 字符
     * @param scene
     * @return
     */
    public static String getLimitTimeIntegerScene(Integer scene){
        JSONObject object = new JSONObject();
        JSONObject sceneObj = new JSONObject();
        JSONObject sceneIdObj = new JSONObject();
        sceneIdObj.put("scene_id",scene);
        sceneObj.put("scene",sceneIdObj);
        object.put("action_info",sceneObj);
        object.put("action_name","QR_LIMIT_SCENE");
        return object.toJSONString();
    }

    /**
     * 永久二维码 字符
     * @param scene
     * @return
     */
    public static String getLimitTimeStringScene(String scene){
        JSONObject object = new JSONObject();
        JSONObject sceneObj = new JSONObject();
        JSONObject sceneIdObj = new JSONObject();
        sceneIdObj.put("scene_str",scene);
        sceneObj.put("scene",sceneIdObj);
        object.put("action_info",sceneObj);
        object.put("action_name","QR_LIMIT_STR_SCENE");
        return object.toJSONString();
    }

    public static String getParam(WeChatQrcodePojo weChatQrcodePojo) {
        if(weChatQrcodePojo.getExpire_seconds() == null ){
            //永久二维码
            if(weChatQrcodePojo.getScene_id() != null){
               return getLimitTimeIntegerScene(weChatQrcodePojo.getScene_id());
            }else{
                return getLimitTimeStringScene(weChatQrcodePojo.getScene_str());
            }
        }else{
            //临时
            if(weChatQrcodePojo.getScene_id() != null){
                return getShortTimeInterScene(weChatQrcodePojo.getExpire_seconds(),weChatQrcodePojo.getScene_id());
            }else{
                return getShortTimeStringScene(weChatQrcodePojo.getExpire_seconds(),weChatQrcodePojo.getScene_str());
            }
        }
    }
}
